<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Product;
use App\Record;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class RecordsController extends Controller
{
    use HasResourceActions;
    public $fields = ['rs', 'it', 'et', 'rh', 'cp', '5k', 'dv', 'da', 'dw', 'dh', 'ds', 'dc', 'df'];
    public $fieldNames = ['信号强度','内部温度','外部温度','湿度','风机转速','继电器状态','交流电压','交流电流','有功功率','有功总电能','功率因素','二氧化碳排量','频率',];


    public function showType(Content $content, $type)
    {
        if (!in_array($type, $this->fields) && $type != 'te') {
            abort(403, '类型错误');
        }
        switch ($type) {
            case 'te':
                $title = '温度分析';
                break;
            case 'rh':
                $title = '湿度分析';
                break;
            case 'dh':
                $title = '用电分析';
                break;
            default:
                $title = '数据分析';
                break;
        }
        return $content
            ->header($title)
            ->description()
            ->body(view('index.index', compact('type')));
    }

    public function map(Content $content)
    {
        $data = Product::All()->map(function ($product) {
            $record = $product->records()->latest()->first();
            return [
                'name' => $product->name,
                'lat' => $product->lat,
                'lng' => $product->lng,
                'it' => $record->it,
                'et' => $record->et,
                'rh' => $record->rh
            ];
        });
        Admin::script($this->script($data));
        return $content
            ->header('Index')
            ->description('description')
            ->body(view('index.map'));
    }

    public function script($data)
    {
        return <<<EOT
         $('.box img').click(function(){
        $('.box').hide()
    })
    var res = {$data};

    var center = new qq.maps.LatLng(29.876663,118.710542);
    var map = new qq.maps.Map(document.getElementById('container'),{
        center: center,
        zoom: 7
    });

    getPositon(res, map)

function getPositon(res, map) {
    var infoWin = new qq.maps.InfoWindow({
        map: map
    });
    infoWin.open();

    for(var i of res){

       var maker = new qq.maps.Label({
            position: new qq.maps.LatLng(i.lat,i.lng),
            map: map,
            content:i.name
        });
       qq.maps.event.addListener(
        maker,
        'click',
        function(evt) {
            for(var j of res){
                if(j.lat == evt.latLng.lat && j.lng == evt.latLng.lng){
                    evt.latLng.name = j.name
                    evt.latLng.et = j.et
                    evt.latLng.it = j.it
                    evt.latLng.rh = j.rh
                }
            }
            infoWin.open();
            $('.it').text(evt.latLng.it)
            $('.et').text(evt.latLng.et)
            $('.rh').text(evt.latLng.rh)
            $('.chartLayer a').attr('data-title',evt.latLng.name)
            infoWin.setContent($('#test-temp').html());
            infoWin.setPosition(evt.latLng);
        }
        );
   }
}

    var myChart = null
    $('body').on('click','.chartLayer a',function(){
        var title = $(this).data('name')
        $.ajax({
          url:'http://manager.iyineng.com/treeData',
          type:'post',
          data:{
            title:$(this).data('title'),
            type:title == '温度'?'te':'rh'
          },
          success(res){
            $('.box').show()
                myChart = echarts.init(document.getElementById('main'));
                myChart.clear();
                  // 指定图表的配置项和数据
                  var option = {
                    backgroundColor:'rgba(255,255,255,.8)',
                    title: {
                      text: title == '温度'?'内/外温度':'相对湿度'
                    },
                    tooltip : {
                      trigger: 'axis'
                    },
                    legend: {
                      data:res.titles
                    },
                    xAxis: {
                      data:res.times
                    },
                    yAxis: {},
                    series: res.data
                  };
                  myChart.setOption(option);
          }
        })
    })
EOT;
    }


    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Record);

        $grid->id('Id');
        $grid->product_id('Product id');
        $grid->error('Error');
        $grid->rs('Rs');
        $grid->it('It');
        $grid->et('Et');
        $grid->rh('Rh');
        $grid->cp('Cp');
        $grid->column('5k');
        $grid->dv('Dv');
        $grid->da('Da');
        $grid->dw('Dw');
        $grid->dh('Dh');
        $grid->ds('Ds');
        $grid->dc('Dc');
        $grid->df('Df');
        $grid->created_at('Created at');
        $grid->updated_at('Updated at');

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Record::findOrFail($id));

        $show->id('Id');
        $show->product_id('Product id');
        $show->error('Error');
        $show->rs('Rs');
        $show->it('It');
        $show->et('Et');
        $show->rh('Rh');
        $show->cp('Cp');
        $show->column('5k');
        $show->dv('Dv');
        $show->da('Da');
        $show->dw('Dw');
        $show->dh('Dh');
        $show->ds('Ds');
        $show->dc('Dc');
        $show->df('Df');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Record);

        $form->number('product_id', 'Product id');
        $form->text('error', 'Error');
        $form->number('rs', 'Rs');
        $form->number('it', 'It');
        $form->number('et', 'Et');
        $form->number('rh', 'Rh');
        $form->number('cp', 'Cp');
        $form->text('5k', '5k');
        $form->text('dv', 'Dv');
        $form->text('da', 'Da');
        $form->text('dw', 'Dw');
        $form->text('dh', 'Dh');
        $form->text('ds', 'Ds');
        $form->text('dc', 'Dc');
        $form->text('df', 'Df');

        return $form;
    }
}
